import Vue from 'vue'
import Vuex from 'vuex'
import { getCarList, getUserInfo, getRecommend } from '@/http/index.js'
//getProductList
Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    token: sessionStorage.getItem('zxgToken') ? sessionStorage.getItem('zxgToken') : '',
    isLogin: sessionStorage.getItem('isLogin') ? sessionStorage.getItem('isLogin') : false,
    userInfo: null,
    carList: [],
    recommendList: [],
    badgeCount: 0,
    allChecked: false,
    productList: [],
  },
  getters: {
    getIsLogin(state) {
      return state.isLogin
    },
    getToken(state) {
      return state.token
    },
    getBadgeCount(state) {
      return state.badgeCount
    },
    getCarData(state) {
      return state.carList
    },
    getRecommendData(state) {
      return state.recommendList
    },
    getCountPrice(state) {
      let countPrice = 0
      state.carList.forEach((item) => {
        if (item.checked) {
          countPrice += item.count * item.price
        }
      })
      return countPrice
    },
    // getProductData(state) {
    //   return state.productList
    // },
  },
  mutations: {
    setToken(state, params) {
      sessionStorage.setItem('zxgToken', params.token)
      sessionStorage.setItem('isLogin', true)
      state.token = params.token
      state.isLogin = true
    },
    setUserInfo(state, params) {
      state.userInfo = params
    },
    setCarList(state, params) {
      state.carList = params
    },
    setRecommendList(state, params) {
      state.recommendList = params
    },
    setBadgeCount(state, params) {
      state.badgeCount = params
    },
    setAllChecked(state, params) {
      state.allChecked = params
    },
    // setProductList(state, params) {
    //   state.productList = params
    // },
  },
  actions: {
    // 获取用户信息
    getInfo(state) {
      getUserInfo((res) => {
        // console.log('用户' + res)
        state.commit('setUserInfo', res.data)
      })
    },
    // 获取购物车数据
    getCarDataList(state) {
      getCarList().then((res) => {
        if (res.status === 0) {
          // console.log('购物车' + res)
          const list = res.data
          let count = 0
          list.forEach((ele) => {
            count += ele.count
            ele.checked = false
          })
          state.commit('setBadgeCount', count)
          state.commit('setCarList', list)
        }
      })
    },
    // getProductDataList(state) {
    //   getProductList().then((res) => {
    //     if (res.status === 0) {
    //       const list = res.data
    //       state.commit('setProductList', list)
    //     }
    //   })
    // },
    getRecommendDataList(state) {
      getRecommend().then((res) => {
        let list = null
        // console.log('推荐' + res)
        list = res.data
        state.commit('setRecommendList', list)
        // console.log('数据发送完毕', list)
      })
    },
    // 处理购物车单选
    changeSingleChecked(state) {
      // 全选状态判断
      let checked = true
      state.getters.getCarData.forEach((item) => {
        if (!item.checked) {
          checked = false
          return
        }
      })
      state.commit('setAllChecked', checked)
    },
    changeAllChecked(data) {
      data.state.carList.forEach((item) => {
        item.checked = !data.state.allChecked
      })
      data.commit('setAllChecked', !data.state.allChecked)
    },
  },
})
export default store
